package com.bbc.service.comment.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;

import com.bbc.base.BaseService;
import com.bbc.base.page.MysqlPaginatedArrayList;
import com.bbc.base.page.PaginatedList;
import com.bbc.entity.comment.Comment;
import com.bbc.entity.comment.CommentQuery;
import com.bbc.mapper.comment.CommentMapper;
import com.bbc.service.comment.CommentService;

@Service
public class CommentServiceImpl extends BaseService implements CommentService {
	
	@Autowired
	private transient CommentMapper commentMapper;

	@Override
	public Integer addComment(Comment comment) {
		return commentMapper.addComment(comment);
	}

	@Override
	public List<Comment> findComment(CommentQuery query, int page, int pageSize) {
		PaginatedList<Comment> list = new MysqlPaginatedArrayList<Comment>(page,pageSize);
		list.setTotalItem(commentMapper.findCommentCount(query));
		query.setStartRow(list.getStartRow());
		query.setEndRow(list.getEndRow());
		list.addAll(commentMapper.findComment(query));
		return list;
	}

	@Override
	public Comment findCommentById(Integer commentId) {
		if (commentId == null) {
			return null;
		}
		CommentQuery q = new CommentQuery();
		q.setCommentId(commentId);
		int count = commentMapper.findCommentCount(q);
		if (count == 1) {
			return commentMapper.findComment(q).get(0);
		} else {
			return null;
		}
	}

	@Override
	public boolean deleteComment(final Integer commentId) {
		@SuppressWarnings({ "unchecked", "rawtypes" })
		boolean boo = (Boolean)this.getTransactionTemplate().execute(new TransactionCallback() {
            public Object doInTransaction(TransactionStatus status){
                boolean flag = false;
                try{
                	Comment comment = findCommentById(commentId);
                	if (comment != null) {
                		
                	}
                }catch (Exception e) {
                    flag = false;
                    status.setRollbackOnly();
                }
                return flag;
            }
        });
        return boo;
	}

}
